'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.genRequestWithRetry = exports.genRequest = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _getRequestConfig = require('../ajax/getRequestConfig');

var _final = require('../final');

var _getPromiseValue = require('../getPromiseValue');

var _truest = require('../truest');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var fetchXhr = function fetchXhr(config) {
    return fetch(config.url, config);
};
var genRequest = function genRequest() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        log = _ref.log,
        _ref$showLog = _ref.showLog,
        showLog = _ref$showLog === undefined ? false : _ref$showLog,
        showToast = _ref.showToast,
        loading = _ref.loading,
        _ref$getXhr = _ref.getXhr,
        getXhr = _ref$getXhr === undefined ? fetchXhr : _ref$getXhr,
        _ref$addData = _ref.addData,
        addData = _ref$addData === undefined ? function (data) {
        return data;
    } : _ref$addData,
        _ref$addHeaders = _ref.addHeaders,
        addHeaders = _ref$addHeaders === undefined ? function (header) {
        return header;
    } : _ref$addHeaders,
        _ref$addBody = _ref.addBody,
        addBody = _ref$addBody === undefined ? function (body) {
        return body;
    } : _ref$addBody,
        _ref$responseHandler = _ref.responseHandler,
        responseHandler = _ref$responseHandler === undefined ? function (res) {
        return res;
    } : _ref$responseHandler,
        _ref$errorHandler = _ref.errorHandler,
        errorHandler = _ref$errorHandler === undefined ? function (err) {
        return Promise.reject(err);
    } : _ref$errorHandler;

    var _showToast = showToast;
    var _request = function _request(arg) {
        var _arg$showLoading = arg.showLoading,
            showLoading = _arg$showLoading === undefined ? true : _arg$showLoading,
            _arg$showToast = arg.showToast,
            showToast = _arg$showToast === undefined ? false : _arg$showToast,
            _arg$showSuccess = arg.showSuccess,
            showSuccess = _arg$showSuccess === undefined ? false : _arg$showSuccess,
            _arg$showError = arg.showError,
            showError = _arg$showError === undefined ? true : _arg$showError,
            _arg = (0, _objectWithoutProperties3.default)(arg, ['showLoading', 'showToast', 'showSuccess', 'showError']);

        showLog && log('requesting', _arg);
        var loadingHandler = null;
        if (loading && showLoading) {
            loadingHandler = loading.show();
        }
        var config = (0, _getRequestConfig.getRequestConfig)(_arg);

        var xhr = getXhr(config).then(function (res) {
            loadingHandler && loadingHandler.hide();
            showLog && log('request success', res);
            if (showToast || showSuccess) {
                _showToast && _showToast(res);
            }
            return res;
        }).catch(function () {
            for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
                args[_key] = arguments[_key];
            }

            loadingHandler && loadingHandler.hide();
            var err = args[0];

            showLog && log('request err', err);
            if (showToast || showError) {
                _showToast && _showToast(err);
            }
            return Promise.reject.apply(Promise, args);
        });
        return xhr;
    };
    return function () {
        var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

        var _ref2$headers = _ref2.headers,
            headers = _ref2$headers === undefined ? {} : _ref2$headers,
            _ref2$data = _ref2.data,
            data = _ref2$data === undefined ? {} : _ref2$data,
            body = _ref2.body,
            arg = (0, _objectWithoutProperties3.default)(_ref2, ['headers', 'data', 'body']);

        var preTask = [];
        preTask.push((0, _getPromiseValue.getPromiseValue)(addData(data)));
        preTask.push((0, _getPromiseValue.getPromiseValue)(addHeaders(headers)));
        preTask.push((0, _getPromiseValue.getPromiseValue)(addBody(body)));

        return Promise.all(preTask).then(function () {
            var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [],
                _ref4 = (0, _slicedToArray3.default)(_ref3, 3),
                _ref4$ = _ref4[0],
                data = _ref4$ === undefined ? {} : _ref4$,
                _ref4$2 = _ref4[1],
                headers = _ref4$2 === undefined ? {} : _ref4$2,
                body = _ref4[2];

            return _request((0, _truest.truest)((0, _extends3.default)({}, arg, {
                headers: headers,
                data: data,
                body: body
            })));
        }).then(responseHandler).catch(errorHandler);
    };
};

exports.genRequest = genRequest;
var genRequestWithRetry = function genRequestWithRetry() {
    var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var _ref5$checkRetry = _ref5.checkRetry,
        checkRetry = _ref5$checkRetry === undefined ? function (res) {
        return false;
    } : _ref5$checkRetry,
        _ref5$responseHandler = _ref5.responseHandler,
        responseHandler = _ref5$responseHandler === undefined ? function (res) {
        return res;
    } : _ref5$responseHandler,
        arg = (0, _objectWithoutProperties3.default)(_ref5, ['checkRetry', 'responseHandler']);

    var _request = genRequest(arg);
    var request = function request(arg, isRetrying) {
        return _request(arg).then(function (res) {
            if (isRetrying) {
                return res;
            }
            return (0, _getPromiseValue.getPromiseValue)(checkRetry(res)).then(function (flag) {
                if (flag) {
                    return request(arg, true);
                }
                return res;
            });
        }).then(responseHandler);
    };
    return request;
};
exports.genRequestWithRetry = genRequestWithRetry;
exports.default = genRequest;